package com.hfkh.keyun.controller.frontend;

import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Unit;
import com.hfkh.keyun.service.UnitService;
import com.hfkh.keyun.util.PageResult;
import com.hfkh.keyun.vo.UnitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/unit")
@Api(tags = "单元管理")
public class UnitControllerApp {

    @Resource
    private UnitService unitService;

    @GetMapping("/selUnitList")
    @ApiOperation("根据专业id查询所有单元")
    public ApiResult selUnitList(@RequestParam(required = true,defaultValue = "")Integer majorId){
         List<Unit> units = unitService.selUnitList(majorId);
        return ApiResult.success(units);
    }

    @GetMapping("/selUnitListById")
    @ApiOperation("根据id查询单元详情")
    public ApiResult selUnitListById(Integer id){
        final Unit unit = unitService.getById(id);
        return ApiResult.success(unit);
    }

    @GetMapping("/UnitList")
    @ApiOperation("根据专业id查询所有单元并分页")
    public ApiResult UnitList(@RequestParam(required = true,defaultValue = "")Integer majorId,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer size){
        PageResult<List<Unit>> listPageResult = unitService.UnitList(majorId, page, size);
        return ApiResult.success(listPageResult);
    }

    @GetMapping("/deleteUnit")
    @ApiOperation("删除单元")
    public ApiResult deleteUnit(Integer id){
        unitService.removeById(id);
        return  ApiResult.ok("删除成功", "头像上传成功！");
    }

    @PostMapping("/addUnit")
    @ApiOperation("新增单元")
    public ApiResult addUnit(@RequestBody UnitVo unitVo){
        Unit unit=new Unit();
        BeanUtils.copyProperties(unitVo,unit);
        unitService.save(unit);
        return ApiResult.ok("新增成功", "头像上传成功！");
    }

}
